import { ref, reactive, toRefs } from 'vue'
import request from '@/utils/request.js'

// ======================================
// 1. 请求参数类型定义（修正拼写错误）
// ======================================
interface ClearData { // 修正：cleraData → ClearData
    databaseName: string
}

// ======================================
// 2. 响应结果类型（泛型增强 + 语义化）
// ======================================
interface BaseResponse<T = any> {
    code?: number
    message?: string
    data?: T
    timestamp?: number
}

// ======================================
// 3. 通用请求工具（扩展方法类型）
// ======================================
const baseRequest = async <T>(
    state: {
        loading: boolean
        error: string | null
        response: BaseResponse<T> | null
    },
    url: string,
    method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH', // 扩展支持的HTTP方法
    params?: Record<string, any>,
    data?: Record<string, any>
): Promise<BaseResponse<T>> => { // 明确返回类型
    state.loading = true
    state.error = null
    state.response = null

    try {
        const response = await request({ url, method, params, data })
        state.response = response.data as BaseResponse<T>
        return state.response // 返回处理后的响应数据
    } catch (error: any) {
        state.error = error.message || '请求失败'
        throw error
    } finally {
        state.loading = false
    }
}

// ======================================
// 4. 业务方法 & 状态管理（独立状态，避免冲突）
// ======================================
export const useClearService = () => {
    const clearByNameState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<string> | null,
    })
    const clearAllState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<string> | null,
    })
    // 为clearDeepSeek添加独立状态
    const clearDeepSeekState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<string> | null,
    })

    // -------------------- 清除指定数据库 --------------------
    const clearByName = async (params: ClearData) => { // 修正参数类型
        return baseRequest<string>(
            clearByNameState,
            '/database/delete/clearTableDatabase',
            'DELETE',
            undefined,
            params
        )
    }

    // -------------------- 清除所有数据库 --------------------
    const clearAll = async () => {
        return baseRequest<string>(
            clearAllState,
            '/database/delete/clearAll',
            'DELETE'
        )
    }

    // -------------------- 清除所有AI对话 --------------------
    const clearDeepSeek = async () => {
        return baseRequest<string>(
            clearDeepSeekState, // 使用独立状态
            '/api/deepseek/clear',
            'DELETE'
        )
    }

    // ======================================
    // 5. 导出（保留状态命名空间，避免冲突）
    // ======================================
    return {
        clearAll,
        clearByName,
        clearDeepSeek
    }
}
